package com.market.quote.interfaces.rest.controller;

import com.market.quote.common.CommonStatus;
import com.market.quote.interfaces.job.ProcessQuoteCsvJob;
import com.market.quote.interfaces.rest.query.QueryRequest;
import com.market.quote.interfaces.rest.query.StockInfoRequest;
import com.market.quote.interfaces.rest.query.converter.QueryConverter;
import com.market.quote.interfaces.rest.vo.ResultVO;
import com.market.quote.app.manager.marketManager.MarketManager;
import com.market.quote.modules.stock.domain.dto.StockInfoDTO;
import com.market.quote.modules.stock.domain.dto.StockRequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author duweibin
 * @date 2024/4/22
 * @description 市场行情接口
 */
@RestController
@RequestMapping("/market")
public class MarketController {

    private final MarketManager marketManager;
    private final QueryConverter queryConverter;
    @Resource
    private ProcessQuoteCsvJob processQuoteCsvJob;

    @Autowired
    public MarketController(MarketManager marketManager, QueryConverter queryConverter) {
        this.marketManager = marketManager;
        this.queryConverter = queryConverter;
    }

    /**
     * 根据ID获取股票报价信息
     * @param id  id
     * @return  stock info
     */
    @GetMapping("/get")
    @ResponseBody
    public ResultVO<StockInfoDTO> queryById(@NotBlank Long id) {
        StockInfoDTO stockInfoDTO = marketManager.getStockInfoById(id);
        return ResultVO.of(CommonStatus.OK,stockInfoDTO);
    }

    /**
     * 根据ID删除股票报价信息
     * @param id  id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResultVO<StockInfoDTO> deleteById(@NotBlank Long id) {
        marketManager.deleteStockById(id);
        return ResultVO.of(CommonStatus.OK,null);
    }

    /**
     * 根据条件范围查询股票报价信息
     * @param queryRequest  查询条件
     * @return stock info list
     */
    @PostMapping("/search")
    @ResponseBody
    public ResultVO<List<StockInfoDTO>> search(@Valid @RequestBody QueryRequest queryRequest) {
        StockRequestDTO stockRequestDTO = queryConverter.toStockRequestDTO(queryRequest);
        List<StockInfoDTO> stockInfoDTOList = marketManager.queryStockInfo(stockRequestDTO);
        return ResultVO.of(CommonStatus.OK,stockInfoDTOList);
    }

    /**
     * 保存修改股票报价信息
     * @param stockInfoRequest  stock info
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultVO<Void> save(@RequestBody StockInfoRequest stockInfoRequest) {
        StockInfoDTO stockInfoDTO = queryConverter.toStockInfoDTO(stockInfoRequest);
        marketManager.saveStock(stockInfoDTO);
        return ResultVO.of(CommonStatus.OK,null);
    }

}
